-- building_kill_monster
-- Created by chengb Jul/17/2015
-- 成就：使用建筑杀怪

-- 内部函数声明
local whenMonsterDie;

-- 注册怪物死亡的回调
EventMgr.register("achievements/building_kill_monster", event.MONSTER_DIE_LOGIC, function(args)
    whenMonsterDie(args);
end);

-- 怪物死亡的回调
whenMonsterDie = function(args)
    local monster = args.monster;
    if not monster then
        return;
    end

    -- 获取攻击者信息
    local assailantInfo = monster.dbase:queryTemp("assailant_info");

    if type(assailantInfo) ~= 'table' then
        return;
    end

    local gridType = assailantInfo["grid_type"];

    if type(gridType) ~= "number" then
        local attackerInfo = monster["attackerInfo"];
        if type(attackerInfo) ~= "table" then
            return;
        end

        local source = attackerInfo["source"];
        if type(source) ~= "table" then
            return;
        end

        local pos = source["pos"];
        if type(pos) ~= "number" then
            return;
        end

        local grids = DungeonM.getCurrentDungeon();
        local grid = grids[pos];
        if type(grid) == "table" then
            gridType = grid.type;
        end
    end

    if type(gridType) ~= "number" then
        -- 不是建筑杀敌
        return;
    end 

    -- 玩家不在迷宫中
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("building_kill_monster");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["grid_type"]) == 'number' and 
               ruleArg["grid_type"] ~= gridType then
                -- 建筑类型不符
            	return false;
            end

            -- 如果是陷阱，还需要判断陷阱id是否一致
            if ruleArg["grid_type"] == GRID_TYPE_TRAP then
                if type(ruleArg["trap_id"]) == 'number' and 
                   ruleArg["trap_id"] ~= assailantInfo["trap_id"] then
            		return false;
            	end
            end

            -- 判断是否是需求怪物
            -- 目前需求怪物编号支持两种格式的配置：1.整型  2.数组
            local baseClassId = monster:query("base_class_id")
            if type(ruleArg["base_class_id"]) == 'number' and
               ruleArg["base_class_id"] ~= baseClassId then
                return false;
            end

            if type(ruleArg["base_class_id"]) == 'table' and
                table.indexOf(ruleArg["base_class_id"], baseClassId) == -1 then
                return false;
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};